package cn.com.dhcc.turbo.web.controller.exch;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.dhcc.app.core.base.BaseController;
import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.core.handler.pagerhandler.Pager;
import cn.com.dhcc.app.core.handler.pagerhandler.PagerData;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.pub.core.annotation.TurboMenu;
import cn.com.dhcc.app.pub.core.annotation.TurboMenu.LEAF_MENU;
import cn.com.dhcc.turbo.entity.monitor.QuotaValueHistory;
import cn.com.dhcc.turbo.entity.monitor.QuotaValueOnTime;
import cn.com.dhcc.turbo.entity.registry.AdapterInfo;
import cn.com.dhcc.turbo.entity.registry.DomainInfo;
import cn.com.dhcc.turbo.entity.registry.NodeInfo;
import cn.com.dhcc.turbo.entity.sysmgr.UserInfo;
import cn.com.dhcc.turbo.service.monitor.MonitorService;
import cn.com.dhcc.turbo.service.registry.AdapterInfoService;
import cn.com.dhcc.turbo.service.registry.DomainInfoService;
import cn.com.dhcc.turbo.service.registry.NodeInfoService;
@Controller
@RequestMapping("/exch")
@TurboMenu(belongToMenu=LEAF_MENU.ALERT_MGR)
public class AlertController  extends BaseController {
	
	@SuppressWarnings("deprecation")
	private static Logger logger = Log.getExistLogger(LOGTYPE.TURBO);
	@Autowired
	private MonitorService monitorService;
	@Autowired
	private NodeInfoService nodeService;
	
	@Autowired
	private DomainInfoService doMainService;
	@Autowired
	AdapterInfoService adapterService;
	
	@RequestMapping(value = "/alert_mgr", method = RequestMethod.GET)
	public String alertMgr(Model model) {
		try {
			NodeInfo node = nodeService.getLocalNode();
			List<DomainInfo> domainList= doMainService.list(null);
		
			model.addAttribute("node", node);
			model.addAttribute("domainList", domainList);
		} catch (Exception e) {
			logger.error("获取告警列表失败", e);
			return toAlertPage(model, e.getMessage());
		}
		return "exch/alert_mgr";
	}
	
	@RequestMapping(value = "/alert_list")
	public String list(Model model, Pager pager,String queryType,String ownerId,String type,String alterLevel,HttpSession httpSession) {
		WhereCondition condition = new WhereCondition();
		try {
			NodeInfo node = nodeService.getLocalNode();
			List<DomainInfo> domainList= doMainService.list(null);
			List<AdapterInfo> adapterList = adapterService.getLocalAdapterList();
			UserInfo userInfo = (UserInfo)super.getUserSessionVo(httpSession);
			if("4".equals(userInfo.getSlAuthFlag())){
				ownerId = userInfo.getOwnId();
			}
			model.addAttribute("node", node);
			model.addAttribute("domainList", domainList);
			model.addAttribute("adapterList", adapterList);
			if("".equals(type) || type==null){
				model.addAttribute("monitType", "null");
			}else{
				model.addAttribute("monitType", type);
			}
			if("".equals(ownerId) || ownerId==null){
				model.addAttribute("monitOwnerId", "null");
			}else{
				model.addAttribute("monitOwnerId", ownerId);
			}
			if(!"".equals(ownerId) && ownerId != null && !"".equals(type)){
				condition.and().eq("t.owner_id", ownerId);
			}
			if(!"".equals(alterLevel) && alterLevel != null ){
				condition.and().eq("alter_level", alterLevel);
			}
			condition.orderBy("mtime desc,alter_level desc,t.quota_code");
			if("".equals(queryType) || queryType==null || "ontime".equals(queryType)){
				queryType = "ontime";
				PagerData<QuotaValueOnTime> pagerData = monitorService.alertlist(
						pager, condition);
				model.addAttribute(DATA_KEY, pagerData.getDatas());
				setTotoal(model, pagerData.getTotal());
			}else{
				PagerData<QuotaValueHistory> pagerData = monitorService.historyAlertlist(
						pager, condition);
				model.addAttribute(DATA_KEY, pagerData.getDatas());
				setTotoal(model, pagerData.getTotal());
			}
		} catch (Exception e) {
			logger.error("获取告警列表失败", e);
			return toAlertPage(model, e.getMessage());
		}
		return "exch/alert_list";
	}
	/**
	 * 关闭告警
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/close_alert", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> close(Model model, String id) {
		String msg = "";
		try {
			monitorService.closeAlert(id);
			msg = "关闭成功";
			return jsonSuccessResult(msg);
		} catch (Exception e) {
			msg = "关闭失败";
			logger.error(msg, e);
			return jsonFailResult(msg);
		}
	}
	/**
	 * 批量关闭告警
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = "/batch_close_alert", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> batchClose(@RequestParam("ids") String ids) {
		String msg = "";
		try {
			String[] strIds = ids.split(",");
			monitorService.batchCloseAlert(strIds);
			msg = "批量关闭告警成功";
			return jsonSuccessResult(msg);
		} catch (Exception e) {
			msg = "批量关闭告警失败";
			logger.error(msg, e);
			return jsonFailResult(msg);
		}
	}
}
